package com.banesco.precargadepositos.interfaz.componentes;

import net.rim.device.api.system.Characters;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.FontFamily;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.decor.BackgroundFactory;
import net.rim.device.api.ui.decor.Border;
import net.rim.device.api.ui.decor.BorderFactory;
import net.rim.device.api.ui.text.TextFilter;

public class TextBoxBanesco extends EditField {

	public String label;
	public int _width;
	private boolean _campoTieneError = false;
	private boolean _tienePuntoDecimal;
	private boolean _validarPuntoDecimal = false;
	private boolean _formaParteDeComponenteCedula = false;
	private int _numerosDecimales = 1;
	private int _tamanofuente;
	private String fontFamily = "BBAlpha Sans";
	
	private int _colorBordeNormal = 0X999999;
	private int _colorBordeSelectedClaro = 0XFF9900;
	private int _colorBordeSelectedOscuro = 0XFF6600;
	private int _colorBordeError = Color.RED;
	private int _colorLetra = 0X00466F;
	


	/*Constructor de la Clase*/
	public TextBoxBanesco(long style, int width, int maxSize, int filtro)
	{	
		super(style | FIELD_VCENTER);
		_tienePuntoDecimal = false;
		_width = width;

		setBackground(BackgroundFactory.createSolidBackground(Color.WHITE));
		//setMargin(10, 10, 0, 10);
		setMargin(0,10,0,0);

		XYEdges edges = new XYEdges(1, 1, 1, 1);

		XYEdges colors = new XYEdges(_colorBordeNormal, _colorBordeNormal, _colorBordeNormal, _colorBordeNormal);	

		Border bordeTextBox = BorderFactory.createSimpleBorder(edges, colors, Border.STYLE_SOLID);
		
		setBorder(bordeTextBox);
		setMaxSize(maxSize);

		setFilter(TextFilter.get(filtro));

		try {
			int displayWidth = Display.getWidth();

			if(displayWidth < 480)
				_tamanofuente = 17;
			else
				_tamanofuente = 26;


			FontFamily fontFamily1;
			fontFamily1 = FontFamily.forName(fontFamily);

			this.setFont(fontFamily1.getFont(Font.PLAIN,_tamanofuente));


		} catch (ClassNotFoundException e) {
			System.out.println("Error TextBoxBanesco" + e.getMessage());
			e.printStackTrace();
		}

	}


	public TextBoxBanesco(long style, int width, int maxSize, int filtro, boolean formaParteDeComponenteCedula)
	{	
		this(style, width, maxSize,filtro);

		_formaParteDeComponenteCedula = formaParteDeComponenteCedula;
	}


	public void layout(int width, int height)
	{
		if(_formaParteDeComponenteCedula)
		{
			int BajaResolucion = 250000;
			int newHeigth;	

			if(Display.getWidth() * Display.getHeight() > BajaResolucion)
				newHeigth = 29;
			else
				newHeigth = 24;

			super.layout(width, newHeigth *2);
			setExtent(width, newHeigth);
		}
		else
		{
			super.layout(width, height * 2);
		}
	}


	protected void displayFieldFullMessage()
	{

	}

	/*
	 * Metodo que valida en el caso de montos decimales que solo acepte uno solo
	 */
	public void validaPuntosDecimales()
	{

	}


	protected void paint(Graphics graphics) {
		graphics.setColor(0x1D4175);

		super.paint(graphics);
	}


	protected void onFocus(int direction) 
	{
		XYEdges edges = new XYEdges(1, 1, 1, 1);
		XYEdges colors = new XYEdges(_colorBordeSelectedOscuro, _colorBordeSelectedClaro, _colorBordeSelectedOscuro, _colorBordeSelectedClaro);
		Border bordeTextBox = BorderFactory.createSimpleBorder(edges, colors, Border.STYLE_SOLID);
		
		this.setBorder(bordeTextBox);

		super.onFocus(direction);
	}


	protected void onUnfocus()
	{
		if(_campoTieneError)
		{
			XYEdges edges = new XYEdges(1, 1, 1, 1);
			XYEdges colors = new XYEdges(_colorBordeError, _colorBordeError, _colorBordeError, _colorBordeError);				
			Border bordeTextBox = BorderFactory.createSimpleBorder(edges, colors, Border.STYLE_SOLID);

			this.setBorder(bordeTextBox);
		}
		else
		{
			XYEdges edges = new XYEdges(1, 1, 1, 1);
			XYEdges colors = new XYEdges(_colorBordeNormal, _colorBordeNormal, _colorBordeNormal, _colorBordeNormal);		
			Border bordeTextBox = BorderFactory.createSimpleBorder(edges, colors, Border.STYLE_SOLID);
			
			this.setBorder(bordeTextBox);
		}

		super.onUnfocus();
	}


	public void setTamanoVertical(int tamano)
	{
		this.getExtent().setSize(this.getWidth(), tamano);
	}


	public boolean keyChar(char key, int status, int time) 
	{
		if(_validarPuntoDecimal)
		{
			switch (key) 
			{
			case Characters.COMMA:
				if(!_tienePuntoDecimal)
				{
					_tienePuntoDecimal = true;
					return  super.keyChar(key, status, time);
				}
				else
					return true;

			case '.':
				if(!_tienePuntoDecimal)
				{
					_tienePuntoDecimal = true;
					return  super.keyChar(key, status, time);
				}
				else
					return true;
			case Characters.BACKSPACE:
				super.keyChar(key, status, time);
				return verificaBorrado(key, status, time);
			}
			return  validarCantidadDecimales(key, status, time);
		}
		else
			return super.keyChar(key, status, time);
	}

	public boolean is_validarPuntoDecimal() {
		return _validarPuntoDecimal;
	}


	public void set_validarPuntoDecimal(boolean _validarPuntoDecimal) {
		this._validarPuntoDecimal = _validarPuntoDecimal;
	}

	/*
	 * Metodo que verifica que caracter se borro para determinas si es "." o no para permitir volver
	 * a escribirlo.
	 * Si el caracter "."  no existe, setea el flag tienepuntodecimal a false para permitir
	 * volver a escribirlo
	 */
	private boolean verificaBorrado(char key, int status, int time)
	{
		String texto = getText();
		if(_tienePuntoDecimal)
		{
			boolean borrado = true;

			for (int i=0; i<texto.length(); i++) {

				if (texto.charAt(i) == ',') {
					borrado = false;
					//System.out.println("TIENE PUNTO");
					break;
				}
			}
			if(borrado)
				_tienePuntoDecimal = false;
		}
		return false;
	}

	/*
	 * Metodo que valida que no exista mas de 2 decimales posterior al punto decimal
	 */
	private boolean validarCantidadDecimales(char key, int status, int time)
	{
		String texto = getText();
		int p = texto.indexOf(','); 
		System.out.println("Indice p");
		System.out.println(p);
		if(p == -1) { 
			//return true; //if no '.' found then the fract part can't be too big 
			return super.keyChar(key, status, time);
		} 

		System.out.println("digits");

		int digits = texto.substring(p + 1, texto.length()).length(); 
		System.out.println(digits);
		if(digits > _numerosDecimales) { 
			return true; 
		} else { 
			return super.keyChar(key, status, time);
		} 
	}


	/**
	 * Metodo que limpia el error marcado del textbox y lo vuelve a pintar de rojo
	 */
	public void LimpiarError()
	{
		_campoTieneError = false;
		
		XYEdges edges = new XYEdges(1, 1, 1, 1);
		XYEdges colors = new XYEdges(_colorBordeNormal, _colorBordeNormal, _colorBordeNormal, _colorBordeNormal); //Azul Banesco		
		Border bordeTextBox = BorderFactory.createSimpleBorder(edges, colors, Border.STYLE_SOLID);
		this.setBorder(bordeTextBox);
	}


	/**
	 * Metodo que marca el borde del textbox en rojo para notificar que tiene un error
	 */
	public void MarcarError()
	{
		_campoTieneError = true;
		
		XYEdges edges = new XYEdges(1, 1, 1, 1);
		XYEdges colors = new XYEdges(Color.RED, Color.RED, Color.RED, Color.RED);				
		Border bordeTextBox = BorderFactory.createSimpleBorder(edges, colors, Border.STYLE_SOLID);
		this.setBorder(bordeTextBox);
	}
}
